<template>
  <div class="about">
    <div class="topdb">
      <van-nav-bar title="电影" :left-text="city" @click-left="onClickLeft" />
      <van-tabs v-model="active" @change="xgdy">
        <van-tab title="正在热映"></van-tab>
        <van-tab title="即将上映"></van-tab>
      </van-tabs>
    </div>
    <div class="filmlist">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="我也是有底线的"
        @load="onLoad"
      >
        <div
          class="film"
          v-for="(item, index) in filmlist"
          :key="index"
          @click="djfilm(item.filmId)"
        >
          <div class="filmimg">
            <img :src="item.poster" />
          </div>
          <div class="filmshuoming">
            <div class="shuoming">
              {{ item.name }}
              <div>{{ item.filmType.name }}</div>
            </div>
            <div class="zhuyan">
              主演:<span v-for="item1 in item.actors">{{ item1.name }}</span>
            </div>
            <div class="zhuyan">上映日期:{{ item.premiereAt }}</div>
          </div>
          <div class="bun">
            <van-button v-if="active == 1" type="primary" size="small"
              >预购</van-button
            >
            <van-button v-if="active == 0" type="primary" size="small"
              >购买</van-button
            >
          </div>
        </div>
      </van-list>
    </div>
  </div>
</template>
<style scoped>
.bun {
  float: left;
  margin-top: 26px;
}
.zhuyan span {
  margin-right: 5px;
}
.zhuyan {
  margin-top: 10px;
  color: #7e7b7b;
}
.shuoming div {
  width: 20px;
  height: 20px;
  background: #e5e5e5;
  color: #fff;
  text-align: center;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  vertical-align: middle;
  border-radius: 2px;
}
.shuoming {
  width: 100%;
  height: auto;
}
.filmshuoming {
  width: 67%;
  height: 100%;
  float: left;
  margin-left: 10px;
}
.filmimg img {
  width: 100%;
  height: 100%;
}
.filmimg {
  width: 90px;
  height: 90px;
  float: left;
}
.film {
  width: 100%;
  height: auto;
  padding: 15px 0px;
  box-sizing: border-box;
  border-bottom: 1px solid #e5e5e5;
  overflow: hidden;
}
</style>
<script>
import Vue from "vue";
import { NavBar } from "vant";
import { Tab, Tabs } from "vant";
import { Dialog } from "vant";
import { Button } from "vant";
import { List } from "vant";
Vue.use(List);
Vue.use(Button);
Vue.use(Dialog);
Vue.use(Tab);
Vue.use(Tabs);
Vue.use(NavBar);
import moment from "moment";
export default {
  data() {
    return {
      city: "",
      active: 1,
      filmlist: [],
      loading: false,
      finished: false,
    };
  },
  mounted() {
    if (!localStorage.getItem("token")) {
      this.qr();
    } else {
      this.city = localStorage.getItem("token");
      this.getfilm();
    }
  },
  methods: {
    djfilm(id) {
      this.$router.push({
        path: `about`,
        query: {
          id: id,
        },
      });
    },
    onLoad() {
      this.loading = false;
      this.finished = true;
    },
    xgdy(e) {
      this.active = e;
      this.getfilm();
      console.log(e);
    },
    onClickLeft() {
      this.$router.push({
        path: "xz",
      });
    },
    getfilm() {
      if (this.active == 0) {
        this.$http
          .get(
            `/pkq/getNowPlayingFilmList?cors=T&pageNum=1&cityId=${localStorage.getItem(
              "ip"
            )}`
          )
          .then((res) => {
            console.log(res);
            this.filmlist = res.data.films;
             this.filmlist.forEach((el) => {
            el.premiereAt = moment(el.premiereAt * 1000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
            console.log(el.premiereAt);
          });
          });
      } else if (this.active == 1) {
        this.$http.get(`/pkq/getComingSoonFilmList?cors=T`).then((res) => {
          console.log(res);
          this.filmlist = res.data.films;
          this.filmlist.forEach((el) => {
            el.premiereAt = moment(el.premiereAt * 1000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
            console.log(el.premiereAt);
          });
        });
      }
    },
    getlc() {
      this.$http
        .get(
          "/baidu/location/ip?ak=QTFdckIoV9ZYCNwfAX3VGfDFHyKB320W&coor=bd09ll"
        )
        .then((res) => {
          console.log(res);
          localStorage.setItem("token", res.content.address);
          localStorage.setItem("ip", res.content.address_detail.adcode);
          this.getfilm();
        });
    },
    qr() {
      Dialog.confirm({
        title: "标题",
        message: "是否定位当前城市？",
      })
        .then(() => {
          // on confirm
          this.getlc();
        })
        .catch(() => {
          // on cancel
          this.$router.push({
            path: "xz",
          });
        });
    },
  },
};
</script>
<style scoped>
.about {
  width: 100%;
  height: 100%;
}
.topdb {
  width: 100%;
  height: 90px;
  z-index: 999;
  position: fixed;
  top: 0px;
  left: 0px;
}
.filmlist {
  width: 100%;
  height: calc(100% - 140px);
  position: absolute;
  top: 90px;

  overflow: auto;
}
</style>